﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.Telephony;

namespace C4F.DevKit.Telephony.Controls
{
    [ToolboxItem(true), ToolboxBitmap(typeof(PlaybackAndRecordingPanel))]
    public partial class PlaybackAndRecordingPanel : UserControl
    {
        #region Members

        private TapiManager _tapiManager;
        
        private OpenFileDialog _openFileDialog;
        
        private FolderBrowserDialog _folderBrowserDialog;

        #endregion

        #region Properties

        public TapiManager TapiManager
        {
            get
            {
                return _tapiManager;
            }

            set
            {
                _tapiManager = value;
            }
        }

        [Browsable(true)]
        public bool PlayStopButtonsEnabled
        {
            get
            {
                return this.btnPlay.Enabled;
            }

            set
            {
                this.btnPlay.Enabled = value;
                this.btnStop.Enabled = value;
            }
        }

        [Browsable(true)]
        public bool CallRecordingControlsEnabled
        {
            get
            {
                return txtFileName.Enabled;
            }

            set
            {
                txtFileName.Enabled = value;
                btnDirectory.Enabled = value;
                btnRecordingStart.Enabled = value;
                btnRecordingStop.Enabled = value;
            }
        }

        [Browsable(true)]
        public bool PlayBackControlsEnabled
        {
            get
            {
                return btnStop.Enabled;
            }

            set
            {
                lstBoxFileNames.Enabled = value;
                btnBrowse.Enabled = value;
                btnPlay.Enabled = value;
                btnStop.Enabled = value;
            }
        }

        [Browsable(true)]
        public bool PlaybackEnabled
        {
            get
            {
                return grpBoxPlayRecord.Enabled;
            }

            set
            {
                grpBoxPlayRecord.Enabled = value;
            }
        }

        #endregion

        #region Constructors

        public PlaybackAndRecordingPanel()
        {
            InitializeComponent();
            this.btnRecordingStop.Click += new System.EventHandler(this.btnRecordingStop_Click);
            this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
            this.lstBoxFileNames.SelectedIndexChanged += new System.EventHandler(lstBoxFileNames_SelectedIndexChanged);
            this.btnPlay.Click += new System.EventHandler(this.btnPlay_Click);
            this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
            this.btnRecordingStart.Click += new System.EventHandler(this.btnRecordingStart_Click);
            this.btnDirectory.Click += new System.EventHandler(this.btnDirectory_Click);
            this.rdCallRecording.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
            this.rdPlayBack.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);

            _folderBrowserDialog = new FolderBrowserDialog();
            _openFileDialog = new OpenFileDialog();
        }

        #endregion

        #region Event Handlers

        private void btnRecordingStop_Click(object sender, EventArgs e)
        {
            try
            {
                _tapiManager.StopCallRecording();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            try
            {
                _tapiManager.StopPlayingFile();
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void lstBoxFileNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            PlayStopButtonsEnabled = lstBoxFileNames.Items.Count > 0;
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstBoxFileNames.SelectedItem != null)
                {
                    _tapiManager.PlayFile(lstBoxFileNames.SelectedItem.ToString());
                }
                else
                {
                    MessageBox.Show("Please select file to play.", "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            _openFileDialog.Filter = "wav files (*.wav)|*.wav";
            _openFileDialog.RestoreDirectory = true;
            _openFileDialog.Multiselect = true;
            if (_openFileDialog.ShowDialog() == DialogResult.OK)
            {
                lstBoxFileNames.Items.Clear();
                lstBoxFileNames.Items.AddRange(_openFileDialog.FileNames);
            }
        }

        private void btnRecordingStart_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtFileName.Text == String.Empty)
                    MessageBox.Show("File name not specified.", "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    string targetFilePath = String.Empty;
                    targetFilePath += _folderBrowserDialog.SelectedPath + "\\" + txtFileName.Text;
                    targetFilePath = targetFilePath.Replace("\\\\", "\\");
                    targetFilePath = targetFilePath.Trim('\\');
                    _tapiManager.RecordCall(targetFilePath);
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, "Telephony Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnDirectory_Click(object sender, EventArgs e)
        {
            _folderBrowserDialog.ShowDialog();
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (rdCallRecording.Checked == true)
            {
                CallRecordingControlsEnabled = true;
                PlayBackControlsEnabled = false;
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (rdPlayBack.Checked == true)
            {
                PlayBackControlsEnabled = true;
                CallRecordingControlsEnabled = false;
            }
        }

        #endregion
    }
}
